package com.freeter.JsonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.freeter.test.TimeTest;

public class FastJsonUtil {

	public static void main(String[] args) throws Exception {
		String jsonText = Txt2String.txt2String("data2.txt");
		Long nowTime = TimeTest.nowTime();
		List list = string2ListMap(jsonText);
		TimeTest.endTime(nowTime);
		 

		 
		/*String aa = "{\"name\":\"jack\",\"age\":\"20\"}";
		Map mp = string2Object(aa, Map.class);*/

	}

	/*
	 * 生成json JavaBean、List<JavaBean>、List<String>、List<Map<String,Object>>
	 */
	public static String json2String(Object obj) {
		return JSON.toJSONString(obj);
	}

	/*
	 *   map javabean
	 */
	public static <T> T string2Object(String jsonText, Class cls) {
		T t = null;
		try {
			t = (T) JSON.parseObject(jsonText, cls);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return t;
	}

	public static List string2List(String jsonText, Class cls) {
		List list = new ArrayList();
		try {
			list = JSON.parseArray(jsonText, cls);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	public static List string2ListMap(String jsonstring) {
		List<Map<String, Object>> listMap = new ArrayList();
		try {
			listMap = JSON.parseObject(jsonstring, new TypeReference<List<Map<String, Object>>>() {
			});

		} catch (Exception e) {
			// TODO: handle exception
		}

		return listMap;
	}

}
